<template>
	<view class="page">
		<view class="head">
			<image src="/static/logo.jpg" mode="widthFix"></image>
			<text>瑶都老年食堂</text>
			<text>申请使用</text>
		</view>
		<text class="title">以下信息</text>
		<view class="desc">
			<image src="/static/restaurant/user/choose.png" mode="widthFix"></image>
			<text>你的帐号信息(昵称、头像、地区及性别)</text>
		</view>

		<view class="btns">
			<!-- <button open-type="getPhoneNumber" @getphonenumber="handLogin">同意</button> -->
			<button @tap="submit">微信授权登录</button>
			<view @tap="login" class="cancel">账号密码登录</view>
		</view>

		<view class="xieyi" @tap="radioChange">
			<radio :value="value" :checked="value" color="#FA802A" style="transform:scale(0.7)" />
			<text>若手机号未注册将进入注册流程，注册即为同意</text> <text class="text"
				@tap.stop="$wanlshop.to('/pages/restaurant/common/article/article?id=1')">《用户服务协议》</text><text class="text"
				@tap.stop="$wanlshop.to('/pages/restaurant/common/article/article?id=2')">《隐私政策协议》</text>
		</view>

		<uni-popup ref="popup" @change="change">
			<view class="zheshow">
				<view class="cen_ter">
					<view class="box_At">
						<view class="box_At_text">获取您的昵称、头像</view>
						<view class="box_At_co">获取用户头像、昵称，主要用于完善个人资料，向用户提供更好使用体验</view>
						<view class="box_B" style="border-top:1px solid #f3f3f3 ;">
							<view class="acvter">头像</view>
							<button class="acvter_all" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"
								style="display: flex;align-items: center;flex: 1;">
								<view v-if="avatar" class="img">
									<image :src="$wanlshop.oss(avatar)"></image>
								</view>
								<view v-else class="mast">请选择头像</view>
							</button>
							<!-- <view v-if="avatar" class="img">
								<image :src="avatar"></image>
							</view> -->
						</view>
						<view class="box_B">
							<view class="acvter">昵称</view>
							<input class="acvter_all" type="nickname" :value="nickname" @blur="bindblur" placeholder="请输入昵称"
								placeholder-class="placeholder-class" placeholderStyle="color:#ababab;font-weight:normal;" />
						</view>
						<view class="Brn_S">
							<view class="btn_btns" @click="colse">取消</view>
							<button class="btn" @tap="handLogin">
								<view class="btn">保存</view>
							</button>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				show: false,
				url: null,
				value: false,

				avatar: '',
				nickname: ''
			}
		},
		computed: {
			...mapState(['user', 'common'])
		},
		async onLoad(option) {
			if (option.url) {
				this.url = decodeURIComponent(option.url)
			}
			uni.login({
				provider: 'weixin',
				success: async (loginRes) => {
					let res = await this.$api.checkwxlogin({
						login_code: loginRes.code
					});
					if (res.code == 1) {
						if (res.data.is_user == 1) {
							this.show = true
							this.avatar = res.data.user.avatar
							this.nickname = res.data.user.nickname
						} else {
							this.show = false;
						}
					}
				}
			})
		},
		methods: {
			login() {
				let url = encodeURIComponent(this.url)
				uni.redirectTo({
					url:'/pages/user/auth/login?url=' + url
				})
			},
			submit() {
				if (!this.value) return this.$wanlshop.msg('请阅读并同意下面协议')
				if(this.show) {
					this.handLogin()
				} else {
					this.$refs.popup.open('bottom')
				}
			},
			change(e) {

			},
			onChooseAvatar(e) {
				let that = this
				console.log(e.detail)
				uni.uploadFile({
					url: `${that.$config.baseUrl}/upfile/image`,
					filePath: e.detail.avatarUrl,
					name: 'file',
					success: (uploadFileRes) => {
						if (JSON.parse(uploadFileRes.data).code == 1) {
							that.avatar = JSON.parse(uploadFileRes.data).data.path;
						}
					}
				});
			},
			upload() {
				let token = uni.getStorageSync("token")
				let that = this
				uni.chooseImage({
					sizeType: ['original'],
					count: 1,
					async success(res) {
						uni.uploadFile({
							url: `${that.$config.baseUrl}/upfile/image`,
							filePath: res.tempFilePaths[0],
							header: {
								token: token
							},
							name: 'file',
							success: (uploadFileRes) => {
								if (JSON.parse(uploadFileRes.data).code == 1) {
									that.avatar = JSON.parse(uploadFileRes.data).data.path;
								}
							}
						});
					}
				})
			},
			bindblur(e) {
				this.nickname = e.detail.value
			},
			colse() {
				this.$refs.popup.close()
			},
			async handLogin(e) {
				// console.log(1111)
				// return false;
				uni.login({
					provider: 'weixin',
					success: async (loginRes) => {
						let res = await this.$api.wxlogin({
							login_code: loginRes.code,
							nickname: this.nickname,
							avatar: this.avatar
						});
						if (res.code == 1) {
							let data = res.data
							this.$wanlshop.msg('登录成功');
							this.$store.dispatch('user/login', data.userinfo);
							this.$store.dispatch('user/setToken', data.userinfo.token);
							if (!this.common.store) {
								uni.reLaunch({
									url: '/pages/user/store/store'
								})
							} else {
								setTimeout(() => {
									if (this.url) {
										if (this.url == '/pages/home' || this.url == '/pages/user') {
											uni.switchTab({
												url: this.url
											});
										} else {
											uni.redirectTo({
												url: this.url
											})
										}
									} else {
										this.$wanlshop.on('/pages/home')
									}
								}, 1500);
							}
						}
					},
					fail: (e) => {

					}
				});
			},
			back() {
				uni.navigateBack()
			},
			radioChange(e) {
				this.value = !this.value
			}
		}
	}
</script>
<style lang="less">
	page {
		background-color: #fff;
	}
</style>

<style lang="scss" scoped>
	.zheshow {
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.3);
		position: fixed;
		top: 0;
		left: 0;
		display: flex;
		align-items: center;
		align-items: flex-end;

		.mast {
			margin-top: 6rpx;
			color: #ababab;
		}

		.Brn_S {
			width: 70%;
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 10rpx auto;
		}

		.btn_btns {
			width: 300rpx;
			height: 80rpx;
			background: antiquewhite;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 10rpx;
			margin-right: 70rpx;
			background-color: #fafafa;
			color: #FA802A;
		}

		.imgs {
			position: absolute;
			right: 6%;
			width: 32rpx;
			height: 32rpx;
		}

		.img {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;

			image {
				width: 100%;
				height: 100%;
				border-radius: 50%;
			}
		}

		.cen_ter {
			width: 100%;
			height: 600rpx;
			border-top-left-radius: 30rpx;
			border-top-right-radius: 30rpx;
			background-color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;

			.box_At {
				width: 90%;
				height: 92%;
				margin-top: 20rpx;
				display: flex;
				flex-direction: column;

				.box_At_text {
					font-weight: bold;
					font-size: 30rpx
				}

				.box_At_co {
					font-size: 28rpx;
					color: #ababab;
					margin-top: 24rpx;
				}

				.box_B {
					width: 100%;
					height: 120rpx;
					border-bottom: 1px solid #f3f3f3;
					display: flex;
					align-items: center;
					margin-top: 12rpx;

					.acvter_all {
						font-size: 28rpx;
						color: #ababab;
						margin-left: 80rpx;
					}
				}

				.btn {
					width: 300rpx;
					margin: 35rpx auto;
					height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background-color: #FA802A;
					color: #FFFFFF;
					border-radius: 10rpx;
					font-size: 30rpx;
				}
			}
		}

		button {
			border-radius: 30rpx;
			height: 80rpx !important;
			padding-left: 0 !important;
			padding-right: 0 !important;
			background-color: rgba(0, 0, 0, 0) !important;
			color: #ababab !important;
			font-family: PingFang SC !important;
		}

		button:after {
			top: 0;
			left: 0;
			border: 1px solid rgba(0, 0, 0, 0) !important;
			-webkit-transform: scale(.5);
			transform: scale(.5);
			-webkit-transform-origin: 0 0;
			transform-origin: 0 0;
			box-sizing: border-box;
			border-radius: 10px;
		}
	}
</style>
<style lang="less" scoped>
	.page {
		.head {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			padding: 38rpx 42rpx;

			image {
				width: 72rpx;
				height: 72rpx;
				border-radius: 50%;
			}

			text {
				color: #333333;
				font-size: 32rpx;
				display: inline-block;
				margin-left: 20rpx;
			}
		}

		.title {
			color: #333333;
			font-size: 48rpx;
			padding: 0 42rpx;
		}

		.desc {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin-top: 30rpx;
			padding: 0 42rpx;
			margin-bottom: 594rpx;

			image {
				width: 36rpx;
				height: 36rpx;
			}

			text {
				color: #333333;
				font-size: 32rpx;
				display: inline-block;
				margin-left: 10rpx;
			}
		}
	}

	.btns {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 40rpx;
		// position: fixed;
		// bottom: 300rpx;
		// left: 50%;
		// margin-left: -167rpx;

		button {
			width: 534rpx;
			height: 90rpx;
			background: #FA802A;
			border-radius: 50rpx;
			color: #FFFFFF;
			font-size: 36rpx;
			line-height: 90rpx;
			text-align: center;
			margin-bottom: 20rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 10rpx;
			}
		}

		view {
			width: 534rpx;
			height: 90rpx;
			background: #F0F0F0;
			border-radius: 50rpx;
			color: #FA802A;
			font-size: 36rpx;
			line-height: 90rpx;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 10rpx;
			}
		}
	}

	.xieyi {
		padding: 0 30rpx;
		font-size: 28rpx;
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		align-items: center;

		text {
			color: #999;

			&.text {
				color: #FA802A;
			}
		}
	}
</style>